package de.jonasbs.breakblock.res;

import java.io.IOException;
import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public enum ResSound {

	BALLCOLLISSION("beeppure.wav"), GAMEOVER("gameover.wav"), LIFELOST("lifelost.wav"), LEVELENDED("levelended.wav");

	public static enum Volume {
		MUTE, NORMAL
	}

	public static Volume volume = Volume.NORMAL;
	private Clip clip;

	ResSound(String soundFileName) {
		try {
			soundFileName = "sounds/" + soundFileName;
			System.out.println("Sound loading: " + soundFileName);
			URL url = this.getClass().getResource(soundFileName);
			System.out.println(url);
			AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
			System.out.println("loading soundfile successful");
			clip = AudioSystem.getClip();
			clip.open(audioInputStream);
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (LineUnavailableException e) {
			e.printStackTrace();
		}
	}

	public void play() {
		if (volume != Volume.MUTE) {
			if (clip.isRunning())
				clip.stop();
			clip.setFramePosition(0);
			clip.start();
		}
	}

	static void init() {
		values();
	}
}